Computers / Programming / Projects / Code Formatter / Type Cache

The Type Cache is used to store already loaded type definitions. This avoids having to load and parse type definitions multiple times. This is also needed as language transitions from previous types may still be valid.

Code

TypeCache.h

h type icon
Type: Header file
Language: C++
TypeCache.h

TypeCache.cpp

cpp type icon
Type: Code file
Language: C++
TypeCache.cpp

The TypeCache class loads type information and then stores it in case it is requested again. The types vector stores the type data. The langs vector stores all the language transitions from types that have been loaded so far as a language transition may be valid even if it doesn’t belong to the current type. For example php switches to html on load but adds several language transitions from html back to php.

The LoadLanguage(std::string type) method is used to load a type. It starts by converting the type name to all lowercase and then comparing it to the list of already loaded types. If it finds a match it returns it. Otherwise it loads the type and then adds it to the types vector and then adds the type's language transitions to the langs vector before returning the new type.

The FindStartLang(std::string line, int pos, TypeIdPair typeId) method runs through the langs vector to see if any of the language transitions are valid given the line, position and current Type and state. If a valid transition is found it is returned, otherwise null is returned to indicate no transition was found.